# -*- coding: utf-8 -*-

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import SIGNAL
from PyQt4.Qt import QRect, QDesktopWidget
from PyQt4.QtCore import QCoreApplication, QEventLoop
from ui_projectselect import Ui_ProjectSelect
from TrainCsvWindow import TrainCsvWindow
from TestCsvWindow import TestCsvWindow
from subprocess import *
from mainfunctions import *
from githg import *
from cvssvn import *

connection = sqlite.connect('commits.db')
cursor = connection.cursor()

projectName = ""
projectVersion = 0

class ProjectSelect(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.ui = Ui_ProjectSelect()
        self.ui.setupUi(self)
        self.browser = None
                
        self.train = TrainCsvWindow()
        self.train.hide()
        
        self.test = TestCsvWindow()
        self.test.hide()
        
        available_geom = QDesktopWidget().availableGeometry()
        current_geom = self.frameGeometry()
        self.setGeometry(available_geom.width() / 2 - current_geom.width() / 2, available_geom.height() / 2 - current_geom.height() / 2, current_geom.width(), current_geom.height());
        
        self.connect(self.ui.projSelectOkBtn, SIGNAL("clicked()"), self.pickProject)
        self.connect(self.ui.projSelectCancelBtn, SIGNAL("clicked()"), self.cancelButton)

    def setBrowser(self, browser):
        self.browser = browser
        self.train.browser = self.browser
        self.test.browser = self.browser
        
    def cancelButton(self):
        self.hide()        

    def listProjects2(self):
        listProjects2(self.browser)

    def pickProject(self):
        if (len(self.ui.projName.text()) != 0) or (len(self.ui.projVersion.text()) != 0):
            projectName = str(self.ui.projName.text())
            projectVersion = str(self.ui.projVersion.text())
            isSuccessful = pickProject(self.ui.projName.text(), self.ui.projVersion.text(), self.browser)

            if(isSuccessful == 5555):
                self.hide()
                isSuccessful = 0
                self.ui.projName.clear()
                self.ui.projVersion.clear()
                
                self.browser.append("Train and Test Csv Files are going to be created...\n\n\nThese files will be related with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                self.train.exec_()
                self.test.exec_()
